package dav.mobile.garuda.states;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Delay;
import com.badlogic.gdx.scenes.scene2d.actions.FadeIn;
import com.badlogic.gdx.scenes.scene2d.actions.Sequence;
import com.badlogic.gdx.scenes.scene2d.actors.Button;
import com.badlogic.gdx.scenes.scene2d.actors.Image;
import com.badlogic.gdx.scenes.scene2d.actors.Button.ClickListener;

import dav.mobile.garuda.components.Constants;
import dav.mobile.garuda.components.Profile;
import dav.mobile.garuda.components.ResourceManager;
import dav.mobile.garuda.components.SoundManager;
import dav.mobile.garuda.components.Utilities;

//State information
public class StateClear extends State implements InputProcessor {
	//Attributes
	String 	m_Header;
	int 	m_Score;
	String	m_Information;
	
	int HeaderX;
	int HeaderY;
	int ScoreX;
	int ScoreY;
	
	float m_Time;
	boolean m_DrawInfo;
	
	//Stage
	Stage m_Stage;
	
	Image m_Aira;
	Image m_Goal;
	Image m_Menu;
	
	Button toMenu;
	Button toRetry;
	Button toContinue;
	
	//Floor
	int	m_FloorY;
	
	//Font
	BitmapFont m_Font;
	
	//Constructor
	public StateClear(final String game_place, final int game_num, final int place, boolean finish, int score, String info) {
		//Set attribute
		m_Header 		= (finish) ? "Cleared" : "Failed";
		m_Score			= score;
		m_Information	= info;
		
		//Set font
		m_Font = (finish) ? ResourceManager.m_FontClear : ResourceManager.m_FontFailed;
		
		HeaderX = (Constants.GAME_WIDTH - m_Font.getBounds(m_Header).width) / 2;
		HeaderY = Constants.GAME_HEIGHT - 130;

		ScoreX = HeaderX;
		ScoreY = HeaderY - m_Font.getBounds(m_Header).height - 15;
		
		m_Time = 0;
		m_DrawInfo = false;
		
		//Create stage
		m_Stage = new Stage(Constants.GAME_WIDTH, Constants.GAME_HEIGHT, true);
		
		//Create background
		Image bg = new Image("Clear Background", ResourceManager.vn_Backgrounds[place]);
		m_Stage.addActor(bg);
		
		//Set floor
		m_FloorY = 10;
		
		//Display sprite
		m_Aira = new Image("aira", ResourceManager.m_AiraSprite);
		m_Aira.x = 40;
		m_Aira.y = m_FloorY;
		m_Stage.addActor(m_Aira);
		
		TextureRegion goal = null;
		if (game_place.equals("epilog")) {
			if ((game_num==3)||(game_num==4)) goal = ResourceManager.m_ButoSprite;
			else if (game_num==5) goal = ResourceManager.m_GardapatiSprite;
		} else if (place==Constants.GAME_PLACE_PRAMBANAN) {
			goal = ResourceManager.m_GaluvastraSprite;
		} else if (place==Constants.GAME_PLACE_BUNAKEN) {
			goal = ResourceManager.m_3DewiSprite;
		} else if (place==Constants.GAME_PLACE_TOBA) {
			goal = ResourceManager.m_GatotKacaSprite;
		} else if (place==Constants.GAME_PLACE_BORNEO) {
			goal = ResourceManager.m_HanomanSprite;
		}
		
		m_Goal = null;
		if (goal!=null) {
			m_Goal = new Image("goal", goal);
			m_Goal.x = 480 - m_Goal.width - 40;
			m_Goal.y = m_FloorY;
			m_Stage.addActor(m_Goal);
		}
		
		//Display menu
		m_Menu = new Image("menu", ResourceManager.m_PopupBackground);
		m_Menu.x = 480 - m_Menu.width - 10;
		m_Menu.action(Sequence.$(Delay.$(FadeIn.$(1.5f), 2.0f)));
		m_Stage.addActor(m_Menu);
		
		//Display button
		toMenu = new Button("menu", ResourceManager.m_ClearMenuButton, ResourceManager.m_ClearMenuButtonPressed);
		toMenu.x = (finish) ? 150 : 185;
		toMenu.y = 35;
		toMenu.action(Sequence.$(Delay.$(FadeIn.$(1.5f), 2.0f)));
		toMenu.clickListener = new ClickListener() {
			@Override
			public void clicked(Button arg0) {
				SoundManager.PlaySFX(SoundManager.m_MenuSound);
				
				SetState(new StateMenu());
			}
		};
		m_Stage.addActor(toMenu);
		
		toRetry = new Button("menu", ResourceManager.m_ClearRetryButton, ResourceManager.m_ClearRetryButtonPressed);
		toRetry.x = (finish) ? 205 : 240;
		toRetry.y = 35;
		toRetry.action(Sequence.$(Delay.$(FadeIn.$(1.5f), 2.0f)));
		toRetry.clickListener = new ClickListener() {
			@Override
			public void clicked(Button arg0) {
				SoundManager.PlaySFX(SoundManager.m_MenuSound);
				
				SetState(new StateGame(game_place, game_num));
			}
		};
		m_Stage.addActor(toRetry);
		
		toContinue = null;
		if (finish) {
			toContinue = new Button("menu", ResourceManager.m_ClearContinueButton, ResourceManager.m_ClearContinueButtonPressed);
			toContinue.x = 260;
			toContinue.y = 40;
			toContinue.action(Sequence.$(Delay.$(FadeIn.$(1.5f), 2.0f)));
			toContinue.clickListener = new ClickListener() {
				@Override
				public void clicked(Button arg0) {
					SoundManager.PlaySFX(SoundManager.m_MenuSound);
					
					//Enable next place if needed
					if (place==Constants.GAME_PLACE_PROLOG&&game_num==1) {
						Profile.getInstance().EnablePlace(Constants.GAME_PLACE_PRAMBANAN);
					} else if (place==Constants.GAME_PLACE_PRAMBANAN&&game_num==3) {
						Profile.getInstance().EnablePlace(Constants.GAME_PLACE_TOBA);
					} else if (place==Constants.GAME_PLACE_BUNAKEN&&game_num==3) {
						Profile.getInstance().EnablePlace(Constants.GAME_PLACE_EPILOG);
						Utilities.WriteJSON(Constants.GAME_PROFILE_FILE, Profile.getInstance().CreateJSON());
						
						//Go to epilog
						SetState(new StateScene("epilog-1"));
					} else if (place==Constants.GAME_PLACE_TOBA&&game_num==3) {
						Profile.getInstance().EnablePlace(Constants.GAME_PLACE_BORNEO);
					} else if (place==Constants.GAME_PLACE_BORNEO&&game_num==3) {
						Profile.getInstance().EnablePlace(Constants.GAME_PLACE_BUNAKEN);
					}
					
					//Save
					Utilities.WriteJSON(Constants.GAME_PROFILE_FILE, Profile.getInstance().CreateJSON());
					
					//Set state
					SetState(new StateScene(game_place+"-"+(game_num+1)));
				}
			};
			m_Stage.addActor(toContinue);
		}
		
		//Set input processor
		Gdx.input.setInputProcessor(this);
	}

	@Override
	public void Update() {
		float delta = Gdx.graphics.getDeltaTime();
		if (m_Time<3.5f) m_Time +=delta;
		else m_DrawInfo = true;
		
		m_Stage.act(delta);
	}

	@Override
	public void Render() {
		m_Stage.draw();
		
		if (m_DrawInfo) {
			m_SpriteBatch.begin();
			m_Font.draw(m_SpriteBatch, m_Header, HeaderX, HeaderY);
			ResourceManager.m_FontScore.draw(m_SpriteBatch, "Score : " + m_Score, ScoreX, ScoreY);
			//ResourceManager.m_FontScene.draw(m_SpriteBatch, m_Information, 0, 0);
			m_SpriteBatch.end();
		}
	}

	@Override
	public void Dispose() {
		super.Dispose();
		
		m_Stage.removeActor(m_Aira);
		if (m_Goal!=null) m_Stage.removeActor(m_Goal);
		m_Stage.removeActor(m_Menu);
		
		m_Stage.removeActor(toMenu);
		m_Stage.removeActor(toRetry);
		if (toContinue!=null) m_Stage.removeActor(toContinue);
		
		m_Stage.dispose();
	}

	@Override
	public boolean keyDown(int keycode) {
		
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		
		return false;
	}

	@Override
	public boolean touchDown(int x, int y, int pointer, int button) {
		m_Stage.touchDown(x, y, pointer, button);
		
		return false;
	}

	@Override
	public boolean touchUp(int x, int y, int pointer, int button) {
		m_Stage.touchUp(x, y, pointer, button);
		
		return false;
	}

	@Override
	public boolean touchDragged(int x, int y, int pointer) {
		m_Stage.touchDragged(x, y, pointer);
		
		return false;
	}

	@Override
	public boolean touchMoved(int x, int y) {
		
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		
		return false;
	}

}
